package com.txzt.gaea.provide.service.biz;

import com.alibaba.druid.support.json.JSONUtils;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.api.client.AdminUserRemoteService;
import com.txzt.gaea.dto.UserBatchDetailQueryDTO;
import com.txzt.gaea.vo.AdminUserVO;
import com.txzt.gaea.vo.UserDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminUserApiService {

    @Value("${roleConfig.adminRoleId}")
    String adminRoleId;

    @Autowired
    private AdminUserRemoteService adminUserRemoteService;

    /**
     * @param ids 用户IDS
     * @return key为ID，value为用户对象
     */
    public Map<Long, UserDetailVO> getUserDetailVos(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        ids = ids.stream().distinct().collect(Collectors.toList());
        UserBatchDetailQueryDTO dto = new UserBatchDetailQueryDTO();
        dto.setToolCode("file_manage");
        dto.setToolVersion("V0.0.1");
        dto.setUserIds(ids);
        RtnInfo<List<UserDetailVO>> rtnInfo = adminUserRemoteService.batchDetailQuery(dto);
        if (null == rtnInfo || 0 != rtnInfo.getCode()) {
            log.error("调用门户接口报错", JSONUtils.toJSONString(ids));
            return new HashMap<>();
        }
        return rtnInfo.getData().stream().collect(Collectors.toMap(UserDetailVO::getId, a -> a));
    }


    public boolean checkAdminUser(AdminUserVO adminUserVO) {
        for (String roleIdStr : adminUserVO.getRoleid().split(",")) {
            if (roleIdStr.equalsIgnoreCase(adminRoleId)) {
                return true;
            }
        }
        return false;
    }
}
